package jp.tonosama.komoki.SimpleGolfScorer2.data;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.widget.Toast;

import java.util.List;

/**
 * Market
 * 
 * @author Komoki
 */
final class Market {

    /**  */
    public static final int NO_RESULT = -1;

    /**  */
    public static final String TAG = "ChartDroid Minimal Example";

    /**  */
    public static final String MARKET_PACKAGE_DETAILS_PREFIX = "market://details?id=";
    /**  */
    public static final String CHARTDROID_PACKAGE_NAME = "com.googlecode.chartdroid";
    /**  */
    public static final Uri MARKET_CHARTDROID_DETAILS_URI = Uri.parse(MARKET_PACKAGE_DETAILS_PREFIX
            + CHARTDROID_PACKAGE_NAME);

    /**  */
    public static final String APK_DOWNLOAD_DETAILS_PAGE_PREFIX = //
    "http://code.google.com/p/chartdroid/downloads/detail?name=";
    /**  */
    public static final String APK_DOWNLOAD_URL_PREFIX = //
    "http://chartdroid.googlecode.com/files/";
    /**  */
    public static final String APK_DOWNLOAD_FILENAME_CHARTDROID = "Chartdroid-1.9.10.apk";
    /**  */
    public static final Uri APK_DOWNLOAD_URI_CHARTDROID = Uri.parse(APK_DOWNLOAD_URL_PREFIX
            + APK_DOWNLOAD_FILENAME_CHARTDROID);

    /**
     * コンストラクタ
     */
    private Market() {
        //
    }

    /**
     * This wrapper function first checks whether an intent is available. If it is not, then the
     * Android Market is launched (if available) to download the appropriate package. On the other
     * hand, if the intent is available, and if a non-negative request code is passed, the Intent is
     * launched with startActivity(). Otherwise, the Intent is launched with
     * startActivityForResult()
     * 
     * @param context context
     * @param intent intent
     * @param requestCode requestCode
     * @param packageName packageName
     */
    public static void intentLaunchWithMarketFallback(final Activity context, final Intent intent,
            final int requestCode, final String packageName) {
        if (isIntentAvailable(context, intent)) {
            if (requestCode < 0) {
                context.startActivity(intent);
            } else {
                context.startActivityForResult(intent, requestCode);
            }
        } else {
            // Launch market intent
            Intent i = getMarketDownloadIntent(packageName);
            if (isIntentAvailable(context, i)) {
                context.startActivity(i);
            } else {
                Toast.makeText(context, "Android Market not available.", Toast.LENGTH_LONG).show();
            }
        }
    }

    /**
     * getMarketDownloadIntent
     * 
     * @param packageName packageName
     * @return intent
     */
    public static Intent getMarketDownloadIntent(final String packageName) {
        Uri marketUri = Uri.parse(MARKET_PACKAGE_DETAILS_PREFIX + packageName);
        return new Intent(Intent.ACTION_VIEW, marketUri);
    }

    /**
     * isIntentAvailable
     * 
     * @param context context
     * @param intent intent
     * @return true:available false:not available
     */
    public static boolean isIntentAvailable(final Context context, final Intent intent) {
        final PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }
}
